package com.ask.forMe.mq.producer;

import cn.hutool.json.JSONUtil;
import com.ask.forMe.model.message.UserBehaviorEvent;
import com.ask.forMe.model.entity.OrderInfo;
import com.ask.forMe.model.enums.BehaviorType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserBehaviorHandler {
    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    /**
     * 处理购买事件
     *
     * @param userId
     * @param itemId
     * @param orderInfo
     */
    public void handlePurchase(Long userId, Long itemId, OrderInfo orderInfo) {
        // 1. 创建用户行为事件
        UserBehaviorEvent event = new UserBehaviorEvent();
        event.setUserId(userId);
        event.setItemId(itemId);
        event.setType(BehaviorType.PURCHASE);
        event.setTimestamp(System.currentTimeMillis());

        // 2. 发送到kafka
        kafkaTemplate.send("user-behaviors", event);
        log.info("发送购买行为事件->消息队列，user-item:{}-{}", userId, itemId);
    }

    /**
     * 处理收藏事件
     *
     * @param userId
     * @param itemId
     */
    public void handleFavorite(Long userId, Long itemId) {
        // 1. 创建用户行为事件
        UserBehaviorEvent event = new UserBehaviorEvent();
        event.setUserId(userId);
        event.setItemId(itemId);
        event.setType(BehaviorType.FAVORITE);
        event.setTimestamp(System.currentTimeMillis());
        event.setContext(null);


        // 2. 发送到kafka
        kafkaTemplate.send("user-behaviors", event);
    }
}
